package com.iiiu.payment.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.iiiu.payment.domain.dto.PageQueryDTO;
import com.iiiu.payment.domain.dto.PaymentDTO;
import com.iiiu.payment.domain.entity.Payment;
import com.iiiu.payment.service.PaymentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import com.iiiu.payment.common.Result;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 花销记录控制器
 */
@Tag(name = "花销记录管理", description = "花销记录相关API")
@RestController
@RequestMapping("/api/payment")
@Validated
@SaCheckLogin
public class PaymentController {

    @Autowired
    private PaymentService paymentService;

    @Operation(summary = "分页查询花销记录", description = "分页查询花销记录，支持关键词搜索和条件筛选")
    @GetMapping
    public IPage<Payment> getPaymentPage(@Valid PageQueryDTO pageQueryDTO) {
        IPage<Payment> paymentPage = paymentService.getPaymentPage(pageQueryDTO);
        return paymentPage;
    }

    @Operation(summary = "根据用户ID查询花销记录列表", description = "获取指定用户的所有花销记录")
    @GetMapping("/user/{userId}")
    public List<Payment> getByUserId(
            @Parameter(description = "用户ID") @PathVariable @NotNull Long userId) {
        List<Payment> payments = paymentService.getByUserId(userId);
        return payments;
    }

    @Operation(summary = "根据ID查询花销记录详情", description = "根据花销记录ID查询详细信息")
    @GetMapping("/{id}")
    public PaymentDTO getPaymentById(
            @Parameter(description = "花销记录ID") @PathVariable @NotNull Long id,
            @Parameter(description = "用户ID") @RequestParam @NotNull Long userId) {
        PaymentDTO paymentDTO = paymentService.getPaymentById(id, userId);
        
        if (paymentDTO != null) {
            return paymentDTO;
        }
            return null;

    }

    @Operation(summary = "创建花销记录", description = "创建新的花销记录")
    @PostMapping
    public Payment createPayment(
            @Valid @RequestBody Payment payment) {
        Payment createdPayment = paymentService.createPayment(payment);
        return createdPayment;
    }

    @Operation(summary = "更新花销记录", description = "更新花销记录信息")
    @PutMapping("/id")
    public Payment updatePayment(
            @Parameter(description = "花销记录ID") @PathVariable @NotNull Long id,
            @Valid @RequestBody Payment payment) {
        payment.setId(id);
        Payment updatedPayment = paymentService.updatePayment(payment);
        return updatedPayment;
    }

    @Operation(summary = "删除花销记录", description = "软删除花销记录")
    @DeleteMapping("/{id}")
    public void deletePayment(
            @Parameter(description = "花销记录ID") @PathVariable @NotNull Long id,
            @Parameter(description = "用户ID") @RequestParam @NotNull Long userId) {
        boolean deleted = paymentService.deletePayment(id, userId);

    }

    @Operation(summary = "批量删除花销记录", description = "批量软删除花销记录")
    @DeleteMapping("/batch")
    public void batchDeletePayments(
            @Parameter(description = "用户ID") @RequestParam @NotNull Long userId,
            @RequestBody List<Long> ids) {
        boolean deleted = paymentService.batchDeletePayments(ids, userId);

    }

    @Operation(summary = "统计用户总花销", description = "统计指定用户的总花销金额")
    @GetMapping("/total/{userId}")
    public Map<String, BigDecimal> getTotalAmount(
            @Parameter(description = "用户ID") @PathVariable @NotNull Long userId) {
        BigDecimal totalAmount = paymentService.getTotalAmountByUserId(userId);
        return Map.of("totalAmount", totalAmount);
    }

    @Operation(summary = "按分类统计花销", description = "统计指定用户在指定分类下的花销金额")
    @GetMapping("/total/category")
    public Map<String, BigDecimal> getTotalAmountByCategory(
            @Parameter(description = "用户ID") @RequestParam @NotNull Long userId,
            @Parameter(description = "分类ID") @RequestParam @NotNull Long categoryId) {
        BigDecimal totalAmount = paymentService.getTotalAmountByCategoryId(userId, categoryId);
        return Map.of("totalAmount", totalAmount);
    }

    @Operation(summary = "按支付方式统计花销", description = "统计指定用户使用指定支付方式的花销金额")
    @GetMapping("/total/payment-method")
    public Map<String, BigDecimal> getTotalAmountByPaymentMethod(
            @Parameter(description = "用户ID") @RequestParam @NotNull Long userId,
            @Parameter(description = "支付方式ID") @RequestParam @NotNull Long paymentMethodId) {
        BigDecimal totalAmount = paymentService.getTotalAmountByPaymentMethodId(userId, paymentMethodId);
        return Map.of("totalAmount", totalAmount);
    }
}